﻿using BaseDll;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HalconDotNet;
namespace UserData
{
    public class MeasureValue
    {
        public double dOriTestHighValue;
        public double dOriDispZPos;
        public double dOriDispPinHigh;

    }
    public class MesureHighMgr
    {
        private MesureHighMgr()
        {

        }
        private Dictionary<string, MeasureValue> dic_MeasureValue = new Dictionary<string, MeasureValue>();
        private static object obj = new object();
        private static MesureHighMgr mesureHighMgr;
        /// <summary>
        /// 获取测高数据管理句柄
        /// </summary>
        /// <returns></returns>
        public static MesureHighMgr GetInstance()
        {
            if (mesureHighMgr == null)
            {
                lock (obj)
                {
                    mesureHighMgr = new MesureHighMgr();
                    return mesureHighMgr;
                }

            }
            else
                return mesureHighMgr;
        }

        public void AddMesureHigh(string str_Name)
        {
            if (!dic_MeasureValue.ContainsKey(str_Name))
                 dic_MeasureValue.Add(str_Name, new MeasureValue());
        }


        /// <summary>
        /// 读取测高标定数据
        /// </summary>
        /// <returns></returns>
        public int Read()
        {
            string currentFile = AppDomain.CurrentDomain.BaseDirectory + @"\config\MesureData.json";
            if (!File.Exists(currentFile))
            {
                Save();
            }
            Object obs = AccessJosnSerializer.JsonToObject(currentFile, typeof(Dictionary<string, MeasureValue>));
            if (obs != null)
            {
                dic_MeasureValue = (Dictionary<string, MeasureValue>)obs;
            }
            return 0;
        }
        /// <summary>
        /// 保存测高标定数据
        /// </summary>
        /// <returns></returns>
        public int Save()
        {
            string currentNozzleFile = AppDomain.CurrentDomain.BaseDirectory + @"\config\MesureData.json";
            bool a = AccessJosnSerializer.ObjectToJson(currentNozzleFile, dic_MeasureValue);
            return 0;

        }
        /// <summary>
        /// 镭射测量值
        /// </summary>
        /// <param name="highPos"></param>
        public void SetOriTestHighValue(string strName, double Value)
        {
            if(dic_MeasureValue.ContainsKey(strName))
            {
                dic_MeasureValue[strName].dOriTestHighValue = Value;
            }
        }
        /// <summary>
        /// 镭射测量时Z轴坐标
        /// </summary>
        /// <param name="highPos"></param>
        public void SetOriDispZPos(string strName, double highPos)
        {
            if (dic_MeasureValue.ContainsKey(strName))
            {
                dic_MeasureValue[strName].dOriDispZPos = highPos;
            }          
        }
        /// <summary>
        /// 针头探高Z轴坐标
        /// </summary>
        /// <param name="highPos"></param>
        public void SetOriDispPinHigh(string strName, double highPos)
        {
            if (dic_MeasureValue.ContainsKey(strName))
            {
                dic_MeasureValue[strName].dOriDispPinHigh = highPos;
            }       
        }
        public double GetNowNozzleHigh(string strName, double dNowTestHigh, double dNowAssZPos)
        {
            if (dic_MeasureValue.ContainsKey(strName))
            {   //测高仪距离越大，数值越大，Z轴正向向上
                //针头-镭射测量偏差 = 探高Z高度 +( 镭射测高标定值 + 实际测高值) + (实际测高Z轴位置-镭射测高标定位Z轴位置)
                double Pin_Laser_Offset = (dic_MeasureValue[strName].dOriTestHighValue - dNowTestHigh) + (dNowAssZPos - dic_MeasureValue[strName].dOriDispZPos);

                //最终高度 = 测量值-标定测量值 +当前Z高度+（针头/镭射测量偏差）
                return dic_MeasureValue[strName].dOriDispPinHigh + Pin_Laser_Offset;
            }
            return 0;
        }


    }



}
